package com.oglvip.business.conf.spring;

import com.oglvip.business.consts.common.Consts;
import com.oglvip.business.extend.auth.RememberMeService;
import com.oglvip.business.extend.auth.RestAuthenticationProvider;
import com.oglvip.business.extend.auth.ValidCodeFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.RememberMeAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter;

/**
 * @auther hanchao
 * @date 2016/11/27 0:03
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private RestAuthenticationProvider restAuthenticationProvider;
    @Autowired
    private RememberMeAuthenticationProvider rememberMeAuthenticationProvider;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/account/**","/validcode/**","/base/**").permitAll()
                .antMatchers("/apply/**","/upload/**").hasAnyRole(Consts.USER_UNREVIEW,Consts.USER)
                .anyRequest().hasRole(Consts.USER)
                .and()
                .formLogin()
                .loginPage("/account/tologin")
                .loginProcessingUrl("/account/login")
                .successForwardUrl("/account/success")
                .failureForwardUrl("/account/fail")
                .and()
                .logout()
                .permitAll()
                .and()
                .addFilterAfter(new RememberMeAuthenticationFilter(this.authenticationManager(),new RememberMeService()),UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(new ValidCodeFilter("/account/login"), UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(rememberMeAuthenticationProvider);
        auth.authenticationProvider(restAuthenticationProvider);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //取消拦截静态资源
        web.ignoring().antMatchers("/","/**/*.html","/**/*.css","/**/*.js","/**/*.ico","/img/**","/fonts/**","/l10n/**");
    }

    @Bean
    public RememberMeAuthenticationProvider rememberMeAuthenticationProvider(){
        return new RememberMeAuthenticationProvider(Consts.REMEMBER_COOKIE);
    }


    /**
     * controller中使用
     * @return
     */
    public AuthenticationManager getAuthenticationManager(){
        try {
            return this.authenticationManager();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
